<?php
namespace Admin\Model;

use Think\Model;

class LenderModel extends Model
{
    protected $connection = 'XYC';
    protected $trueTableName = 'lender';
    protected $_validate = array(
        array('shop_idenfier', 'require', '商户号不能为空'),
        array('name', 'require', '姓名不能为空'),
        array('mobile',"/[1][3,4,5,7,8][0-9]\d{8}/",'手机号格式有误'),
        array('idcard', "/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/", '身份照号格式错误'),
        array('idcard', "check_idcard_is_exist", '该身份照号已存在','0','callback'),
        array('signature', 'require', '签名不能为空'),
    );

    /**
     * 检测该商户下面的出借人是否已存在
     * @param $param
     * @return bool
     */
    public function check_idcard_is_exist($param){
        $res=$this->WHERE(['shop_idenfier'=>$_SESSION['shop_identifier'],'idcard'=>$param,'is_delete'=>'1'])->getField('id');
        if(empty($res)){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 删除出借人
     * @param string $idcard
     * @return boolean|string
     */
    public function del_lender($idcard){
        $res=$this->WHERE(['idcard'=>$idcard,'shop_idenfier'=>$_SESSION['shop_identifier'],'is_delete'=>'1'])->getField('id');
        if(!empty($res)){
            $res=$this->WHERE(['idcard'=>$idcard,'shop_idenfier'=>$_SESSION['shop_identifier']])->setField('is_delete','-1');
            if($res!==false){
                return true;
            }else{
                return '系统错误，请联系管理员';
            }
        }else{
            return '未发现出借人';
        }
    }


}